package w20rest.java;

import org.junit.Test;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.client.ClientHttpRequest;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.web.client.RequestCallback;
import org.springframework.web.client.ResponseExtractor;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;

import java.io.IOException;
import java.util.Collections;

public class PagingGetOrders {

    public static final String AP = "http://localhost:8888/";

    @Test
    public void test() {
//        testGetAllOrders();
        testGetSomeOrders();
    }

    private void testGetSomeOrders() {
        String uri = AP;
        uri += "orders";

        final UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromUriString(uri);
        uriComponentsBuilder.query("size=8&page=3");
        final UriComponents encode = uriComponentsBuilder.build().encode();
        final String uriString = encode.toUriString();
        System.out.println(uriString);

        final RestTemplate restTemplate = RestUtil.createRestTemplate(Collections.EMPTY_MAP);

        final ResponseExtractor<String> responseExtractor = new ResponseExtractor<String>() {

            public String extractData(ClientHttpResponse response) throws IOException {
                final HttpHeaders headers = response.getHeaders();
                System.out.println(response.getStatusCode() + " " + response.getStatusText());
                System.out.println(headers);
                return null;
            }
        };

        final RequestCallback requestCallback = new RequestCallback() {
            public void doWithRequest(ClientHttpRequest request) throws IOException {
//                final HttpHeaders headers = request.getHeaders();
//                System.out.println(headers);
            }
        };

        String s = restTemplate.execute(uriString, HttpMethod.GET, requestCallback, responseExtractor);
//        System.out.println(s);
        System.out.println(restTemplate.getForObject(uriString, String.class));

    }

    private void testGetAllOrders() {
        String uri = AP;
        uri += "orders";

        final UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromUriString(uri);
        final UriComponents encode = uriComponentsBuilder.build().encode();
        final String uriString = encode.toUriString();
        System.out.println(uriString);

        final RestTemplate restTemplate = RestUtil.createRestTemplate(Collections.EMPTY_MAP);

        final ResponseExtractor<String> responseExtractor = new ResponseExtractor<String>() {

            public String extractData(ClientHttpResponse response) throws IOException {
                final HttpHeaders headers = response.getHeaders();
                System.out.println(response.getStatusCode() + " " + response.getStatusText());
                System.out.println(headers);
                return null;
            }
        };

        final RequestCallback requestCallback = new RequestCallback() {
            public void doWithRequest(ClientHttpRequest request) throws IOException {
//                final HttpHeaders headers = request.getHeaders();
//                System.out.println(headers);
            }
        };

        String s = restTemplate.execute(uriString, HttpMethod.GET, requestCallback, responseExtractor);
        System.out.println(s);
        System.out.println(restTemplate.getForObject(uriString, String.class));

    }


}
